<?php
/*
 * @Descripttion: 
 * @version: 1.0
 * @Author: Sam
 * @Date: 2022-01-27 17:44:36
 * @LastEditors: Sam
 * @LastEditTime: 2022-07-11 10:20:48
 */
namespace app\admin\controller;
use think\facade\Db;
use app\common\BaseController;
use app\admin\model\UserCommission as UserCommissionModel;

class UserCommission extends BaseController{
    


    /**
     * 首页
     */
    public function index(){
        $page  = (int)request()->param('page', 0);
        $limit  = (int)request()->param('limit', 10);
        $search = request()->param();
        $where = [];
        if(isset($search["user"]) && $search["user"]!="") $where[] = ["name|mobile","like","%{$search["user"]}%"];
        if(isset($search["status"]) && $search["status"]!="") $where[] = ["status","=",$search["status"]];
        if(isset($search["order_sn"]) && $search["order_sn"]!="") $where[] = ["order_sn","like","%{$search["order_sn"]}%"];
        $query   = UserCommissionModel::with('user')->where($where)->append(['status_text'])->order('id desc');
        $total  = $query->count();
        $data   = $query->page($page, $limit)->order('id desc')->select();
        return $this->success('', [
            'start' => $page,
            'limit' => $limit,
            'data'  => $data,
            'total' => $total,
        ]);

        if(IS_EXCEL) {
            $db = UserCommissionModel::with(['user','store'])->where($where)->append(['status_text'])->order('id asc');
            big_array_to_csv($db, '分销员佣金', [
                'title' => '门店',
                'name'       => '姓名',
                'mobile'    => '手机号',
                'order_sn'   => '订单编号',
                'order_pay_price' =>'订单金额',
                'user_commission' => '佣金',
                'status'   => ['状态',function($row){
                    if($row['status'] ==-1){
                        return '待入账';
                    }else{
                        return '已入账';
                    }
                }]
            ]);
        }

    }

    /**
     * 导出excell
     * @return void
     * @author Sam
     *
     */
    public function user_commission_export(){
        $search = request()->param();
        $search = request()->param();
        $where = [];
        if(isset($search["user"]) && $search["user"]!="") $where[] = ["name|mobile","like","%{$search["user"]}%"];
        if(isset($search["status"]) && $search["status"]!="") $where[] = ["status","=",$search["status"]];
        if(isset($search["order_sn"]) && $search["order_sn"]!="") $where[] = ["order_sn","like","%{$search["order_sn"]}%"];
        $query   = UserCommissionModel::with('user')->where($where)->append(['status_text'])->order('id desc');
        big_array_to_csv($query,'text.csv',[
            'id'=>'ID',
            'name'=>'分销员',
            'mobile'=>'手机号',
            'order_sn'=>'订单编号',
            'order_pay_price'=>'订单金额',
            'user_commission'=>'佣金',
            'status_text'=>'状态',
        ]);
        
    }

}